from datetime import datetime, timedelta


class Monitor:
    def __init__(self, threshold):
        self.threshold_dict = threshold
        # self.format_str = "%Y-%m-%d %H:%M:%S"
        self.event = {}
        self.event_time_list = []
        self.time_list = []

    def flow_change(self, data, last_data):
        flow_change_flag = 0
        # 流量变化较大
        flow = self.threshold_dict["flow"]
        up_flow = last_data["data"]["up_flow_num"] - data["data"]["up_flow_num"]
        down_flow = last_data["data"]["down_flow_num"] - data["data"]["down_flow_num"]
        # 不关注上游变化
        if up_flow > flow * last_data["data"]["up_flow_num"]:
            flow_change_flag = 1
        if down_flow > flow * last_data["data"]["down_flow_num"]:
            flow_change_flag = 2

        return flow_change_flag

    # def flow_match(self, data, last_data):
    #     # 匹配流量变化较大
    #     flow = self.threshold_dict["flow"]
    #     match = data["total_match"] - last_data["total_match"]
    #     if match > 10:
    #         if match > flow * last_data["total_match"]:
    #             return True
    #         if match > 20:
    #             return True
    #     return False

    def car_speed(self, data, last_data, vtype="1"):
        # 小客车速度骤降
        speed = self.threshold_dict["speed"]
        if vtype not in data["data"]["type_speed_dict"].keys() or vtype not in last_data["data"]["type_speed_dict"].keys():
            return False
        current_speed = data["data"]["type_speed_dict"][vtype]["mean_speed"]
        last_speed = last_data["data"]["type_speed_dict"][vtype]["mean_speed"]
        speed_diff = last_speed - current_speed
        if speed_diff > speed * last_speed and last_speed >= 60:
            return True
        if current_speed < 60:
            return True
        return False

    def car_slow_num(self, data, vtype="1"):
        # 小客车慢行数量过多
        slow_num = self.threshold_dict["slow_num"]
        if vtype not in data["data"]["type_speed_dict"].keys():
            return False
        num = data["data"]["type_speed_dict"][vtype]["slow_level_count"]
        current_num = num[1] + num[2] + num[3]
        if current_num >= slow_num:
            return True
        return False

    def flow_empty(self, data):
        up_flow = data["data"]["up_flow_num"]
        down_flow = data["data"]["down_flow_num"]
        if up_flow == 0 and down_flow == 0:
            return True
        return False

    def pass_rate(self, data, vtype="1"):
        pass_rate = self.threshold_dict["pass_rate"]
        if vtype not in data["data"]["type_pass_rate_dict"].keys():
            return False
        car_pass_rate = data["data"]["type_pass_rate_dict"][vtype]

        if car_pass_rate["count"] <= 5:
            return False
        current_pass_rate = int(car_pass_rate["pass"] / car_pass_rate["count"] * 100)
        if current_pass_rate < pass_rate:
            return True
        return False

    def interval_num(self, data):
        interval_num = int(data["info"]["dist"] * 1000 / 10.0)
        if data["data"]["interval_dict"] >= interval_num:
            return True
        return False

    def run(self, data, last_data):
        content = "畅通"
        if last_data is not None:
            # 结束时间 datetime类型的时间数据
            event_time = data["data"]["time"]
            # 流量变化标志flow_change_flag，0：正常，1：上游骤降，2：下游骤降
            flow_change_flag = self.flow_change(data, last_data)
            # 车速变化标志speed_flag，True：车速骤降或车速过低，False：正常
            speed_flag = self.car_speed(data, last_data)
            # 慢行车数量标志slow_flag，True：慢行车数量过多，False：正常
            slow_flag = self.car_slow_num(data)
            # 车流量为零标志flow_empty_flag，True：上下游车流量均为零，False：正常
            flow_empty_flag = self.flow_empty(data)
            # 通过率标志pass_flag，True：通过率过低，False：正常
            pass_flag = self.pass_rate(data)
            # 间隔内车数量标志interval_num_flag，True：间隔内车数量过多，False：正常
            interval_num_flag = self.interval_num(data)

            # if flow_empty_flag:
            #     content = "车流量为零，车流中断"
            #     return content
            # if flow_flag == 1:
            #     content = "上游流量骤降"
            #     return content

            if speed_flag:
                if pass_flag:
                    content = "车速过低，通过率过低"
                    return content
                else:
                    content = "车速过低"
                    return content
            if pass_flag:
                if flow_change_flag == 2:
                    content = "下游流量骤降"
                    return content
                content = "通过率过低"
                return content
            con = last_data["last"]
            if con != "畅通" and con is not None:
                if data["data"]["down_flow_num"] < 3:
                    content = "车流中断"
                    return content

        return content


class Congestion:
    def __init__(self):
        self.congestion = "畅通"

    def run(self, mean_speed):
        if mean_speed >= 60:
            self.congestion = "畅通"
        elif mean_speed >= 40:
            self.congestion = "轻度拥堵"
        elif mean_speed >= 20:
            self.congestion = "中度拥堵"
        else:
            self.congestion = "严重拥堵"

        return self.congestion


if __name__ == '__main__':
    print("2222222222222")
